package com.socket.nio.nio.sticky;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Hexudong
 * @date 2025/4/19
 */
@Slf4j
public class HelloWorldServer {

  public static void main(String[] args) throws InterruptedException {
    NioEventLoopGroup boss = new NioEventLoopGroup(1);
    NioEventLoopGroup worker = new NioEventLoopGroup();

    try {
      ServerBootstrap serverBootstrap = new ServerBootstrap();
      serverBootstrap.channel(NioServerSocketChannel.class);
      serverBootstrap.group(boss, worker);
      serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) {
          ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
        }
      });
      ChannelFuture channelFuture = serverBootstrap.bind(8082).sync();
      channelFuture.channel().closeFuture().sync();
    } catch (InterruptedException e) {
      log.error("server error", e);
    } finally {
      boss.shutdownGracefully();
      worker.shutdownGracefully();
    }
  }
}
